package com.gigi.service.manual;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gigi.entity.LikeCommentShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.model.LikeCommentDO;
import com.gigi.model.ListFavoriteRequest;
import com.gigi.service.generated.LikeCommentShiqinService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LikeCommentManageService {

    private final LikeCommentShiqinService likeCommentShiqinService;

    public LikeCommentManageService(LikeCommentShiqinService likeCommentShiqinService) {
        this.likeCommentShiqinService = likeCommentShiqinService;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<LikeCommentShiqinEntity> list(ListFavoriteRequest request) {
        LambdaQueryWrapper<LikeCommentShiqinEntity> wrapper = new LambdaQueryWrapper<LikeCommentShiqinEntity>()
                .eq(LikeCommentShiqinEntity::getUserId, request.getUserId());
        return MultiResponse.ofWithoutTotal(likeCommentShiqinService.list(wrapper));
    }

    /**
     * 点赞/点踩接口
     *
     * @param request
     * @return
     */
    public Response change(LikeCommentDO request) {
        // 没id，直接新增
        if (Objects.isNull(request.getId())) {
            LikeCommentShiqinEntity entity = new LikeCommentShiqinEntity();
            entity.setUserId(request.getUserId());
            entity.setType(request.getType());
            entity.setCommentId(request.getCommentId());
            if (!likeCommentShiqinService.save(entity)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }

        } else {
            // 有id有type，改type
            if (Objects.nonNull(request.getType())) {
                LikeCommentShiqinEntity entity = new LikeCommentShiqinEntity();
                entity.setId(request.getId());
                entity.setType(request.getType());
                if (!likeCommentShiqinService.updateById(entity)) {
                    return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
                }

            } else {

                // 只有id，删
                if (!likeCommentShiqinService.removeById(request.getId())) {
                    return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
                }

            }
        }
        return Response.buildSuccess();
    }

}
